<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
	<meta name="format-detection" content="telephone=no, date=no, address=no, email=no, url=no">
	<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="Expires" content="0">
	{include file="Public:header" /}
	<title>门店首页</title>
	<meta name="description" content="">
	<link rel="stylesheet" href="__WECHATCSS__/main.css">
	<script type="text/javascript">
		(function (win, doc) {
			if (!win.addEventListener) return;
			var html = document.documentElement;
			function setFont() {
				var cliWidth = html.clientWidth;
				html.style.fontSize = 100 * (cliWidth / 750) + 'px';
			}
			win.addEventListener('resize', setFont, false);
			setFont();
		})(window, document);
	</script>
</head>

<body>
	<div class="container">
		<div class="head">
			<div class="PersonalImgNav">
				<div class="nav">
					<img src="{$member_info.headimgurl}" />
				</div>
			</div>
			<div class="map-text">
				您所在位置<br />
				<p>{$storeInfo.name}</p>
			</div>
		</div>
		<form method="POST">
			<div class="shop-content">
				<h1>请填写您要购买的数量</h1>
				{volist name="list" id="v"}
				<div class="YH">
					<img src="{$v.image}" />
					<div>
						{$v.goods_name}&nbsp;<i>￥</i>
						<a>{$v.price|number_format=2}</a>
						<br />
						<ul> 
							<li class="reduce" id="reduce2"></li>
							<li class="input-text">
								<input name="goods_store_id[]" type="hidden" value="{$v.id}" />
								<input name="goods_id[]" type="hidden" value="{$v.goods_id}" />
								<input name="price[]" type="hidden" value="{$v.price}" />
								<input name="goods_num[]" class="goods_num" type="number" value='0' data-price="{$v.price}" pattern="\d*" />
							</li>
							<li class="add" id="add2"></li>
						</ul>
					</div>
				</div>
				{/volist}
				<input type="hidden" name="seat_id" value="0">
				<input type="hidden" name="store_id" value="{$storeInfo.id}">
				<hr />
				<p class="money">
					合计金额：<i>￥</i>
					<a id="ToMoney" name="Money">0.00</a>
					<input type="hidden" id="total_amount" name="total_amount" value="0">
				</p>
			</div>
			<button type="button" class="submit" id="pay_btn">付&nbsp;款</button>
			<!-- 页脚和弹框 -->
			<div class="footer"></div>
		    <div class="alert" style="display: none;">
			    <div class="alert-text">
				    <h3>提示</h3>
				    <span class="alert_content">提示信息</span>
				    <p id="display">OK</p>
			    </div>
		    </div>	

		</form>
	</div>
	<script src="__WECHATASSETS__/jquery.min.js"></script>
	<script src="__WECHATASSETS__/zepto.min.js"></script>
	<script src="__WECHATJS__/main.js"></script>
	{include file="Public:footer" /}

	<script type="text/javascript">
		var jsApiParameters = {
			"appId": "",     //公众号名称，由商户传入     
			"timeStamp": "",         //时间戳，自1970年以来的秒数     
			"nonceStr": "", //随机串     
			"package": "",
			"signType": "MD5",         //微信签名方式：     
			"paySign": "" //微信签名 
		};
		$(function () {
			closeAlert();
			$('#pay_btn').click(function () {
				var goods_store_id = new Array(), goods_id = new Array(), price = new Array(), goods_num = new Array();
				var i = 0;
				var seat_id = $('input[name=seat_id]').val();
				var store_id = $('input[name=store_id]').val();
				var total_amount = $('input[name=total_amount]').val();
				var count = 0;
				$('.input-text').each(function () {
					var j = 0;
					$(this).find('input').each(function () {
						switch (j) {
							case 0:
								goods_store_id[i] = $(this).val();
								j++;
								break;
							case 1:
								goods_id[i] = $(this).val();
								j++;
								break;
							case 2:
								price[i] = $(this).val();
								j++;
								break;
							case 3:
								goods_num[i] = $(this).val();
								if (!isNaN(goods_num[i]) && goods_num[i].length > 0 && parseInt(goods_num[i]) > 0) {
									count += parseInt(goods_num[i]);
								}
								j++;
								break;
						}
					});
					i++;
				});
				if (count <= 0) {
					//弹框
					if($('.alert').css('display')=='none'){
	                	$('.alert').css('display','block');
		                $('.alert_content').empty().append('请选择商品');
	                };
	                // alert('请选择商品');
					return;
				}
				$.ajax({
					url: "{:Url('Order/submitOrder')}",
					data: { goods_store_id: goods_store_id, goods_id: goods_id, price: price, goods_num: goods_num, seat_id: seat_id, store_id: store_id, total_amount: total_amount },
					type: "post",
					dataType: "json",
					success: function (res) {
						if (res.status > 0) {
							jsApiParameters = res.jsApiParameters;
							jsApiCall();
						} else {
							//弹框
							if($('.alert').css('display')=='none'){
			                	$('.alert').css('display','block');
				                $('.alert_content').empty().append(res.msg);
			                };
							// alert(res.msg);
						}
					}
				});
			});
		});
		//调用微信JS api 支付
		function jsApiCall() {
			wx.chooseWXPay({
				timestamp: jsApiParameters.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
				nonceStr: jsApiParameters.nonceStr, // 支付签名随机串，不长于 32 位
				package: jsApiParameters.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
				signType: jsApiParameters.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
				paySign: jsApiParameters.paySign, // 支付签名
				success: function (res) {
					if (res.errMsg == "chooseWXPay:ok") {
						jsApiParameters = {
							"appId": "",     //公众号名称，由商户传入     
							"timeStamp": "",         //时间戳，自1970年以来的秒数     
							"nonceStr": "", //随机串     
							"package": "",
							"signType": "MD5",         //微信签名方式：     
							"paySign": "" //微信签名 
						};
						//弹框
						if($('.alert').css('display')=='none'){
	                	   $('.alert').css('display','block');
		                   $('.alert_content').empty().append('支付成功');
							closeAlert(function(){
								window.location = "{:Url('index/index',['store_id'=>$store_id])}";
							});
	                    };
					} else {
						//弹框
						if($('.alert').css('display')=='none'){
	                	   $('.alert').css('display','block');
		                   $('.alert_content').empty().append('支付失败');
	                    };
	                    // alert('支付失败');
						console.log(JSON.stringify(res));
						//alert('支付失败！' + res.errDesc + res.errMsg);
					}
				},
				fail: function (res) {
					//alert(JSON.stringify(res));
				}
			});
		}
	</script>
</body>

</html>